package com.gethb.prod.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gethb.prod.service.IUserInfAddService;
import com.gethb.prod.service.IUserLoginService;
import com.gethb.prod.vo.AdminInfoOutVo;
import com.gethb.prod.vo.LoginInVo;
import com.gethb.prod.vo.Result;
import com.gethb.prod.vo.UserInVo;
import com.gethb.pub.utils.BaseInVo;
import com.gethb.pub.utils.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin")
public class AdminController {

//    private static final Logger LOG = LoggerFactory.getLogger(AdminController.class);

    private HttpServletRequest request;

    // 用户登陆实现类
    @Resource(name = "userLoginService")
    private IUserLoginService iUserLoginService;

    // 用户注册实现类
    @Resource(name = "userMapperService")
    private IUserInfAddService iUserInfAddService;

    @Autowired
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    /**
     * 用户登陆
     */
    @ResponseBody
    @PostMapping(value = "/login", produces = "application/json;charset=UTF-8")
    public Result<String> userLogin(@RequestBody JSONObject jsonParam) {
        LoginInVo inVo;
        String token;
        try {
            inVo = JSON.toJavaObject(jsonParam, LoginInVo.class);
            token = iUserLoginService.adminLogin(inVo);
        } catch (Exception error) {
            return Result.exception(error.getMessage());
        }
        //
        if (token == null) {
            return Result.badParam("手机号或密码错误");
        } else {
            return Result.success(token);
        }
    }

    @ResponseBody
    @GetMapping(value = "/info")
    public Result<AdminInfoOutVo> getInfo() {

        String userId = ToolsUtils.getUserId(iUserLoginService, request);
        if (userId == null) {
            return Result.tokenInvalid();
        }

        AdminInfoOutVo info = new AdminInfoOutVo();
        info.setName("Jack");
        info.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return Result.success(info);
    }

    /**
     * 用户登出
     */
    @ResponseBody
    @PostMapping(value = "/logout", produces = "application/json;charset=UTF-8")
    public Result<String> logout() {

        String userId = ToolsUtils.getUserId(iUserLoginService, request);
        if (userId == null) {
            return Result.exception("未找到用户");
        } else {
            return Result.success("success");
        }
    }

    /**
     * 用户注册
     */
    @ResponseBody
    @PostMapping(value = "/registerMember", produces = "application/json;charset=UTF-8")
    public Result<String> register(@RequestBody JSONObject jsonParam) {
        UserInVo inVo = BaseInVo.parseJsonStr(jsonParam.toJSONString(), UserInVo.class);
        //
        boolean registered;
        try {
            registered = iUserInfAddService.checkPhoneNo(inVo.getPhone());
        } catch (Exception error) {
            return Result.exception(error.getMessage());
        }
        //
        if (registered) {
            return Result.badParam("该手机号码已经注册会员");
        } else {
            String userId;
            try {
                userId = iUserInfAddService.addUserInf(inVo.getPhone(), inVo.getName(), inVo.getPassword(), inVo.getReferencesId());
            } catch (Exception error) {
                return Result.exception(error.getMessage());
            }
            return Result.success(userId);
        }
    }
}
